<template>
  <FormView
    v-model="entity"
    :title="entity.id ? '修改标准资产型号' : '新建标准资产型号'"
    :toolbar-attrs="{ buttons: ['back', 'save', 'cancel'] }"
    :toolbar-listeners="{ save: persist }">
    <FormHolder ref="form" label-width="110px" :model="entity">
      <FormItemGroup block-title="基本信息">
        <FormTreeSelect
          v-model="entity.ctiId"
          :span="24"
          label="资产分类"
          :options="ctiTreeList"
          prop="ctiId"
          required />
        <FormInput v-model="entity.name" :span="24" prop="name" required label="资产名称" max-length="255" />
        <FormInput v-model="entity.barcode" :span="12" prop="barcode" label="条形码" max-length="255" required />
        <FormInput v-model="entity.model" :span="12" prop="amount" label="规格型号" max-length="255" required />
        <FormInputNumber v-model="entity.amount" :span="12" prop="amount" label="金额" required />
        <FormInput v-model="entity.unit" prop="amount" :span="12" label="计量单位" max-length="255" required />
        <FormInput v-model="entity.photo" :span="24" label="照片">
          <template #default>
            <UploadAvatar
              v-if="entity.unid"
              ref="uploadAvatar"
              :limit="1"
              :data="{ uniqueCode: entity.unid, photo: entity.photo, model: 'asset', classify: 'sau' }"
              @uploadDel="uploadDel"
              @uploadSuccess="uploadSuccess" />
          </template>
        </FormInput>
      </FormItemGroup>
    </FormHolder>
  </FormView>
</template>

<script>
  import { addObj, putObj, getObj, create } from '@/api/asset/fyassetsau'
  import form from '@/vue/mixin/form'
  import { getHeader } from '@/api/attachment/attachment'
  import { fetchCtiTree } from '@/api/asset/fyassetcti'

  export default {
    name: 'SauForm',
    relativeViewId: 'SauTreeView',
    mixins: [form],
    data() {
      return {
        entity: {
          amount: 0,
          barcode: '',
          classified: '',
          classifiedCode: '',
          ctiId: null,
          id: '',
          unid: '',
          model: '',
          name: '',
          photo: '',
          unit: ''
        },
        headers: {},
        ctiTreeList: []
      }
    },
    created() {
      this.init()
    },
    methods: {
      create,
      load: getObj,
      save: addObj,
      update: putObj,
      async init() {
        this.headers = getHeader()
        const res = await fetchCtiTree()
        this.ctiTreeList = res.data.data
      },
      uploadDel() {
        this.entity.photo = ''
      },
      uploadSuccess(file) {
        this.entity.photo = file.url
      }
    }
  }
</script>
